<?php


namespace App\Modules\Order\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class getCenterOrderListCriteria extends BaseCriterias
{

    private $params;

    public function __construct($storeId, $startDate, $endDate, $isList, $user_id = null)
    {
        $this->params = [
            'store_id' => $storeId,
            'start_date' => $startDate,
            'end_date' => $endDate,
            'is_list' => $isList,
            'user_id' => $user_id
        ];
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $user_id = $this->params['user_id'];
        $result = $model
            ->select(['id', 'real_price', 'member_id', 'source', 'finish_time', 'created_at'])
            ->with('member:id,name,face', 'items')
            ->when($user_id, function ($query) use ($user_id){
                $query->where('barber_id',$user_id);
            })
            ->whereDate('created_at', '>=', $this->params['end_date'])
            ->whereDate('created_at', '<=', $this->params['start_date'])
            ->where(['store_id' => $this->params['store_id'], 'status' => config('constant.orderStatus.done')])
            ->orderByDesc('created_at');
        return $this->params['is_list'] ? $result->limit(5) : $result;

    }
}
